/**
 * 
 */
package com.ideeli.webextractor.service;

import static com.ideeli.webextractor.utils.Constants.BASE_URL_LIKE;
import static com.ideeli.webextractor.utils.Constants.LIKE_EMAIL;
import static com.ideeli.webextractor.utils.Constants.LIKE_CNT;

import static com.ideeli.webextractor.utils.Constants.LIKE_EMAIL_PASS;
import static org.junit.Assert.fail;

import java.util.concurrent.TimeUnit;

import org.apache.commons.lang3.StringUtils;
import org.apache.log4j.Logger;
import org.openqa.selenium.Alert;
import org.openqa.selenium.By;
import org.openqa.selenium.NoAlertPresentException;
import org.openqa.selenium.NoSuchElementException;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;

/**
 * @author sa
 * 
 */
public class LikeServiceImpl implements LikeService {

	private static final Logger logger = Logger
			.getLogger(LikeServiceImpl.class);

	private CacheService cacheService;

	private WebDriver driver;

	private boolean acceptNextAlert = true;
	private StringBuffer verificationErrors = new StringBuffer();
	String baseUrl = null;

	public void init() throws Exception
	{
		driver = new FirefoxDriver();
		baseUrl = cacheService.getAppProperty(BASE_URL_LIKE);
		driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
	}

	public void process() throws Exception
	{

		logger.info("Started process");
		
		int lks=6;
		if(StringUtils.isNotEmpty(cacheService.getAppProperty(LIKE_CNT)))
		{
			lks=Integer.getInteger(cacheService.getAppProperty(LIKE_CNT));
		}
		
		for (int i = 0; i <lks ; i++)
		{
			try
			{
				init();
				recur();
				tearDown();

			} catch (Exception e)
			{
				System.out.println("Exception no prob");
				tearDown();
			}
		}

		// driver.findElement(By.xpath("(//button[@value='1'])[4]")).click();

	}

	private void recur() throws Exception
	{
		System.out.println("Start testFaceBookLike");
		driver.get(baseUrl + "/");
		driver.findElement(By.id("email")).sendKeys(cacheService.getAppProperty(LIKE_EMAIL));
		System.out.println("Logging in ");
		driver.findElement(By.id("pass")).sendKeys(cacheService.getAppProperty(LIKE_EMAIL_PASS));
		System.out.println("Send passwords");
		driver.findElement(By.id("u_0_b")).click();
		System.out.println("u_0_b");
		Thread.sleep(1000 * 5);

		driver.findElement(By.id("u_0_27")).click();
		Thread.sleep(1000 * 5);
		System.out.println("u_0_27");
		driver.findElement(By.id("u_0_27")).sendKeys(cacheService.getAppProperty("LIKE_KEYS"));//
		System.out.println("send keys itshop.com.sg");

		Thread.sleep(1000 * 10);
		driver.findElement(By.xpath("(//button[@value='1'])[4]")).click();
		Thread.sleep(1000 * 7);

	}

	public void tearDown() throws Exception
	{
		driver.quit();
		String verificationErrorString = verificationErrors.toString();
		if (!"".equals(verificationErrorString))
		{
			fail(verificationErrorString);
		}
	}

	private boolean isElementPresent(By by)
	{
		try
		{
			driver.findElement(by);
			return true;
		} catch (NoSuchElementException e)
		{
			return false;
		}
	}

	private boolean isAlertPresent()
	{
		try
		{
			driver.switchTo().alert();
			return true;
		} catch (NoAlertPresentException e)
		{
			return false;
		}
	}

	private String closeAlertAndGetItsText()
	{
		try
		{
			Alert alert = driver.switchTo().alert();
			String alertText = alert.getText();
			if (acceptNextAlert)
			{
				alert.accept();
			} else
			{
				alert.dismiss();
			}
			return alertText;
		} finally
		{
			acceptNextAlert = true;
		}
	}

	public CacheService getCacheService()
	{
		return cacheService;
	}

	public void setCacheService(CacheService cacheService)
	{
		this.cacheService = cacheService;
	}

}
